<?php
/**
 * 用户肤质管理 微信H5 页面获取肤质检测记录接口 V2.3
 *
 * Author: hezhizheng
 * Date: 2018/5/10
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Wechat\V2_3;

use App\Http\Controllers\ApiController;
use App\Http\Middleware\WechatAuth;
use App\Http\Requests\Wechat\SkinRecordIndexRequest;
use App\Http\Requests\Wechat\SkinRecordListRequest;
use App\Models\OpenWechat\WechatUser;
use App\Services\OpenWechat\WechatUserRepository;
use App\Services\Repositories\SkinRecord\SkinRecordRepository;

class SkinRecordController extends ApiController
{
    const ROUTES = [
        'index' => 'api.wechat.v2-3.skin.record.index',
        'list'  => 'api.wechat.v2-3.skin.record.list',
    ];

    protected $skinRecordRepository;
    protected $wechatUserRepository;

    public function __construct(SkinRecordRepository $skinRecordRepository,
                                WechatUserRepository $wechatUserRepository)
    {
        $this->skinRecordRepository = $skinRecordRepository;
        $this->wechatUserRepository = $wechatUserRepository;
    }


    /**
     * 用户测肤数据列表
     * @test
     *
     * @param SkinRecordIndexRequest $request
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(SkinRecordIndexRequest $request)
    {
        /** @var WechatUser $wechat_user */
        $wechat_user = WechatAuth::$wechat_user;
        $customer_id = WechatAuth::$customer_id;

        // 微信用户数据
        $this->wechatUserRepository->appendPersonalCardInfo($wechat_user);

        $skin_detector_id = (int) $request->skin_detector_id;

        $skin_record_ids = $this->skinRecordRepository
            ->searchByWechatAndSkinDetector($wechat_user, $skin_detector_id)
            ->modelKeys();

        // Analysis 分析图数据，取最后一次的值
        $analysis = $this->wechatUserRepository->getLastAnalysis($skin_detector_id, $wechat_user, $skin_record_ids, $customer_id);

        //  趋势图数据
        $skin_records = $this->skinRecordRepository->getSkinRecords($skin_detector_id, $skin_record_ids, $customer_id);

        $wechat_user_info = $wechat_user;

        return $this->response(compact('wechat_user_info', 'analysis', 'skin_records'));
    }

    /**
     * 肤质检测数据的展示(详情)
     * @test
     *
     * @param SkinRecordListRequest $request
     *
     * @return array|\Illuminate\Http\Response
     */
    public function skinRecordList(SkinRecordListRequest $request)
    {
        /** @var WechatUser $wechat_user */
        $wechat_user = WechatAuth::$wechat_user;

        $store_ids = $wechat_user->skinRecords->pluck('store_id')->all();

        $skin_detector_id = (int) $request->skin_detector_id;

        $skin_suggests = $this->skinRecordRepository->getCustomerSkinSuggests($skin_detector_id, WechatAuth::$customer_id);

        $skin_record_ids = $this->skinRecordRepository->getSkinRecordIdsForList($skin_detector_id, $wechat_user);

        $skin_records = $this->skinRecordRepository->normalizeSkinRecordList($request, $wechat_user, $skin_record_ids, $store_ids, $skin_suggests);

        return $this->response(compact('skin_records'));

    }
}
